package at.jku.analyzer.interfaces;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public abstract class AnalyzerModule implements AnalyzerModuleInterface{
	
	protected BufferedReader br;
	File file;
	
	public AnalyzerModule(File file){
		this.file = file;
		resetReader();
	}
	
		
	@Override
	public int getLoC() {
		int count = 0; 
		try {
			while(br.readLine()!=null){
				count++;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return count;
		
	}
	
	
	@Override
	public abstract int getLinesOfComments();

	@Override
	public abstract Map<String, Integer> getNumOfKeywords();

	@Override
	public Map<String, Integer> getNumOfWords() {
		HashMap<String, Integer> map =  new HashMap<>();
		return null;
	}
	
	public void resetReader(){
		try {
			br = new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/** 
	 * Reads the keywords of a language from a file.
	 * 
	 * @param file the text file where in each row one key word is entry
	 * @return Returns a map where all keywords of the corresponding language are entried as keyvalues
	 * */
	public Map<String, Integer> getListOfKeywords(File file) throws IOException{
		
		BufferedReader rd = new BufferedReader(new FileReader(file));
		
		HashMap<String, Integer> listOfKeywords = new HashMap<>();
		
		String word = rd.readLine();
		
		while(word != null){
			listOfKeywords.put(word, 0);
			word = rd.readLine();
		}
		
		return listOfKeywords;
	}
	
}
